<template>
  <section id="app">
    <TodoHeaderVue @add="handleAdd"></TodoHeaderVue>
    <TodoMainVue :list="list" @del="handleDel"></TodoMainVue>
    <TodoFooterVue :list="list" @clear="handleClear"></TodoFooterVue>
  </section>
</template>

<script>
import TodoFooterVue from "./components/TodoFooter.vue";
import TodoHeaderVue from "./components/TodoHeader.vue";
import TodoMainVue from "./components/TodoMain.vue";

export default {
  name: "App",
  components: { TodoHeaderVue, TodoMainVue, TodoFooterVue },
  data() {
    return {
      list: JSON.parse(localStorage.getItem("list")) || [
        { id: 1, name: "打篮球" },
        { id: 2, name: "看电影" },
        { id: 3, name: "逛街" },
      ],
    };
  },
  methods: {
    handleAdd(todoName) {
      this.list.unshift({ id: +new Date(), name: todoName });
    },
    handleDel(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    handleClear() {
      this.list = [];
    },
  },
  watch: {
    list: {
      deep: true,
      handler(newVal) {
        localStorage.setItem("list", JSON.stringify(newVal));
      },
    },
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
